﻿using Dhgms.JobHelper.Composition;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dhgms.JobHelper.Service.Model
{
    public class JobCatalog
    {
        private readonly NLog.Logger logger;

        private readonly CompositionContainer container;

        [ImportMany]
        IEnumerable<Lazy<IJob, IJobMetadata>> jobs;

        public JobCatalog()
        {
            var logger = NLog.LogManager.GetCurrentClassLogger();

            var catalog = new AggregateCatalog();

            var path = string.Format("{0}\\{1}",System.Reflection.Assembly.GetExecutingAssembly().CodeBase, "Extensions");

            catalog.Catalogs.Add(new DirectoryCatalog(path));
            this.container = new CompositionContainer(catalog);

            try
            {
                this.container.ComposeParts(this);
            }
            catch (Exception ex)
            {
                logger.ErrorException("Loading of Job Catalog", ex);
            }
        }
    }
}
